from zz import zz_log, zz_code, zz_img_cv, zz_str
import cv2
import uiautomation as auto
from game import zz_game

log = zz_log.of()


# 移动测试

def save_img():
    window = auto.PaneControl(searchDepth=1, ClassName='地下城与勇士')  # search 2 times
    window.SetTopmost(False)
    window.Click()

    img_path = zz_game.get_pc_temp_file()
    window.CaptureToImage(img_path)


def main():
    src_path = './test.png'
    src_path = ''
    if zz_str.is_null_or_empty(src_path):
        save_img()
        src_path = zz_game.get_pc_temp_file()

    player_path = 'game/img/player.png'
    target_path = 'game/img/prize.png'

    r = zz_img_cv.find_img(src_path, player_path)
    if r.err():
        log.info(f"{player_path}:{r.get_errmsg()}")
        return
    player_xy = r.get_data("first")
    r = zz_img_cv.find_img(src_path, target_path)
    if r.err():
        log.info(f"{target_path}:{r.get_errmsg()}")
        return
    target_xy = r.get_data("first")
    log.info(f"player:{player_xy},target:{target_xy}")

    key_arr = zz_game.code_list(player_xy, target_xy, zz_game.GameSpeed.go)
    for item in key_arr:
        key = item['key']
        timeout = item['timeout']
        log.info(f"{key}--{timeout}ms")
        zz_game.click(key, timeout)


if __name__ == '__main__':
    main()
